<?php


namespace app\api\controller;


use app\common\controller\Api;

class UserAddress extends Api
{
    protected $noNeedLogin = [''];
    protected $noNeedRight = ['*'];

    /**
     * 获取地址分页列表
     *
     * @param $page int 页码
     * @param $limit int 每页数据
     */
    public function getAddressPageList(\app\common\model\UserAddressModel $UserAddressModel)
    {
        $page = $this->request->request("page", 1);
        $limit = $this->request->request("limit", 5);
        $user_id = $this->auth->id;
        $return = $UserAddressModel->getPageList($user_id,$page,$limit);
        $this->errorReturn($return['code'],$return['data'],$return['msg']);
    }

    /**
     * 获取地址详情
     *
     * @param $address_id int 地址ID
     */
    public function getAddressDetail(\app\common\model\UserAddressModel $UserAddressModel, \app\api\validate\UserAddressValidate $UserAddressValidate)
    {
        $params = $this->request->post();
        if(!$UserAddressValidate->check($params,[],'getAddressDetail')){
            $this->errorReturn(40001,$params,$UserAddressValidate->getError());
        }
        $user_id = $this->auth->id;
        $return = $UserAddressModel->detail($params,$user_id);
        $this->errorReturn($return['code'],$return['data'],$return['msg']);
    }

    /**
     * 获取用户默认地址
     */
    public function getUserDefaultAddress(\app\common\model\UserAddressModel $UserAddressModel, \app\api\validate\UserAddressValidate $UserAddressValidate)
    {
        $user_id = $this->auth->id;
        $return = $UserAddressModel->UserDefaultAddress($user_id);
        $this->errorReturn($return['code'],$return['data'],$return['msg']);
    }


    /**
     * 添加地址
     *
     * @param $province string 省
     * @param $city string 市
     * @param $area string 区
     * @param $detail string 详细地址
     * @param $name string 姓名
     * @param $mobile string 手机号
     * @param $is_default int 是否默认:0=不是,1=是
     */
    public function addAddress(\app\common\model\UserAddressModel $UserAddressModel, \app\api\validate\UserAddressValidate $UserAddressValidate)
    {
        $params = $this->request->post();
        if(!$UserAddressValidate->check($params,[],'addAddress')){
            $this->errorReturn(40001,$params,$UserAddressValidate->getError());
        }
        $user_id = $this->auth->id;
        $return = $UserAddressModel->addAddress($params,$user_id);
        $this->errorReturn($return['code'],$return['data'],$return['msg']);
    }


    /**
     * 设为默认地址
     *
     * @param $address_id int 地址ID
     */
    public function addDefault(\app\common\model\UserAddressModel $UserAddressModel, \app\api\validate\UserAddressValidate $UserAddressValidate)
    {
        $params = $this->request->post();
        if(!$UserAddressValidate->check($params,[],'addDefault')){
            $this->errorReturn(40001,$params,$UserAddressValidate->getError());
        }
        $user_id = $this->auth->id;
        $return = $UserAddressModel->addDefault($params,$user_id);
        $this->errorReturn($return['code'],$return['data'],$return['msg']);
    }

    /**
     * 删除收货地址
     *
     * @param $address_id int 地址ID
     */
    public function deleteAddress(\app\common\model\UserAddressModel $UserAddressModel, \app\api\validate\UserAddressValidate $UserAddressValidate)
    {
        $params = $this->request->post();
        if(!$UserAddressValidate->check($params,[],'deleteAddress')){
            $this->errorReturn(40001,$params,$UserAddressValidate->getError());
        }
        $user_id = $this->auth->id;
        $return = $UserAddressModel->deleteAddress($params,$user_id);
        $this->errorReturn($return['code'],$return['data'],$return['msg']);
    }

    /**
     * 修改地址
     *
     * @param $address_id int 地址ID
     * @param $province string 省
     * @param $city string 市
     * @param $area string 区
     * @param $detail string 详细地址
     * @param $name string 姓名
     * @param $mobile string 手机号
     * @param $is_default int 是否默认:0=不是,1=是
     */
    public function editAddress(\app\common\model\UserAddressModel $UserAddressModel, \app\api\validate\UserAddressValidate $UserAddressValidate)
    {
        $params = $this->request->post();
        if(!$UserAddressValidate->check($params,[],'editAddress')){
            $this->errorReturn(40001,$params,$UserAddressValidate->getError());
        }
        $user_id = $this->auth->id;
        $return = $UserAddressModel->editAddress($params,$user_id);
        $this->errorReturn($return['code'],$return['data'],$return['msg']);
    }

}